home *** CD-ROM | disk | FTP | other *** search
- /*
- * BitsGetter.cpp
- *
- * Copyright (C) Alberto Vigata - January 2000 - ultraflask@yahoo.com
- *
- * This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
- *
- * FlasKMPEG is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2, or (at your option)
- * any later version.
- *
- * FlasKMPEG is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with GNU Make; see the file COPYING. If not, write to
- * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- */
-
-
- #include "BitsGetter.h"
-
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
-
- CBits::CBits(CBitSource *source, int streamID, int subStreamID)
- {
- src=source;
- ld=new bits_data;
- CBits::streamID = streamID;
- CBits::subStreamID = subStreamID;
-
- }
-
- CBits::~CBits()
- {
- delete ld;
- }
-
- void CBits::Initialize_Buffer(){
-
- audioBufferSize=1024;
- ld->Incnt = 0;
- ld->Rdptr = ld->Rdbfr + audioBufferSize;
- ld->Rdmax = ld->Rdptr;
- ld->Bfr = 0;
- eos=false;
- Flush_Buffer(0); /* fills valid data into bfr */
- };
-
-
- void CBits::Fill_Buffer(){
-
- int Buffer_Level;
- do{
- Buffer_Level =src->ReadPES((unsigned char **)&ld->Rdbfr, &PES);
- }while(((PES.streamID!=streamID) || (PES.subStreamID!=subStreamID)) && Buffer_Level);
-
- audioBufferSize=PES.payloadSize;
- ld->Rdmax=ld->Rdbfr + audioBufferSize;
-
- ld->Rdptr = ld->Rdbfr;
-
-
- /* end of the bitstream file */
- if (Buffer_Level < audioBufferSize || Buffer_Level==0)
- {
- /* just to be safe */
- if (Buffer_Level < 0)
- Buffer_Level = 0;
-
- /* pad until the next to the next 32-bit word boundary */
- while (Buffer_Level & 3)
- ld->Rdbfr[Buffer_Level++] = 0;
-
-
- /* pad the buffer */
- while (Buffer_Level < audioBufferSize)
- {
- ld->Rdbfr[Buffer_Level++] = 0>>24;
- ld->Rdbfr[Buffer_Level++] = 0>>16;
- ld->Rdbfr[Buffer_Level++] = 0>>8;
- ld->Rdbfr[Buffer_Level++] = 0&0xff;
- }
- eos=true;
- }
- }
-
-
- unsigned int CBits::Get_Bits(int N)
- {
- unsigned int Val;
-
- Val = Show_Bits(N);
- Flush_Buffer(N);
-
- return Val;
- }
- unsigned int CBits::Get_Bits1(){
- return Get_Bits(1);
- }
- unsigned int CBits::Show_Bits(int N){
- return ld->Bfr >> (32-N);
- }
- int CBits::Get_Word(){
- int Val;
-
- Val = Get_Byte();
- return (Val<<8) | Get_Byte();
- }
-
- int CBits::Get_Byte(){
- while(ld->Rdptr >= ld->Rdbfr+audioBufferSize)
- {
- //src->ReadPES((char *)ld->Rdbfr,audioBufferSize);
- ld->Rdptr -= audioBufferSize;
- ld->Rdmax -= audioBufferSize;
- }
- return *ld->Rdptr++;
- }
-
-
- void CBits::Flush_Buffer(int N){
- int Incnt;
-
- ld->Bfr <<= N;
-
- Incnt = ld->Incnt -= N;
-
- if (Incnt <= 24)
- {
- if (ld->Rdptr < ld->Rdbfr+audioBufferSize-4)
- {
- do
- {
- ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
- Incnt += 8;
- }
- while (Incnt <= 24);
- }
- else
- {
- do
- {
- if (ld->Rdptr >= ld->Rdbfr+audioBufferSize)
- Fill_Buffer();
- ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
- Incnt += 8;
- }
- while (Incnt <= 24);
- }
- ld->Incnt = Incnt;
- }
-
- #ifdef VERIFY
- ld->Bitcnt += N;
- #endif /* VERIFY */
- }
-